package com.hx.conterller;

import com.hx.dao.EmpDao;
import com.hx.pojo.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

@Controller
public class EmpConterller {
    @Autowired
    private EmpDao empDao;

    //1 查询所有员工信息
    @GetMapping(value = "/getAllEmp")
    public String getAllEmp(Model model){
        Collection<Emp> empList = empDao.getAll();
        System.out.println(empList);
        model.addAttribute("empList",empList);
        return "empList";
    }

    //2 删除员工信息
    @DeleteMapping(value = "/deleteEmp/{id}")
    public String deleteEmp(@PathVariable("id") Integer id){
        empDao.delete(id);
        return "redirect:/getAllEmp";
    }

    //3 添加员工信息
    @PostMapping(value = "/save")
    public String addEmp(Emp emp){
        empDao.save(emp);
        return "redirect:/getAllEmp";
    }

    //4 执行修改先查询
    @GetMapping(value = "/updateEmp/{id}")
    public String uodateEmpById(@PathVariable("id") Integer id,Model model){
        Emp emp = empDao.get(id);
        model.addAttribute("emp", emp);
        return "empUpdate";
    }
    //5 查询数据做修改后提交修改数据
    @PutMapping(value = "/toUpdate")
    public String toUpdate(Emp emp){
        empDao.save(emp);
        return "redirect:/getAllEmp";
    }
}
